//
//  UserSettingsViewController.m
//  HuLaQuan
//
//  Created by hok on 1/20/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "UserSettingsViewController.h"

//cells
#import "UserSwitchSettingCell.h"
#import "ConfirmButtonCell.h"

//logic modal
#import "UserSettingsEngine.h"

@interface UserSettingsViewController ()

@property (nonatomic,strong) NSArray *items;

@property (nonatomic,strong) UserSettingsEngine *userSettingsEngine;

@end

static NSString * const UserSwitchSettingCellIdentifier = @"UserSwitchSettingCell";
static NSString * const ConfirmButtonCellIdentifier = @"ConfirmButtonCell";

@implementation UserSettingsViewController
- (UserSettingsEngine *)userSettingsEngine
{
    if (!_userSettingsEngine) {
        _userSettingsEngine = [[UserSettingsEngine alloc] init];
    }
    return _userSettingsEngine;
       
}
- (NSArray *)items
{
    if (!_items) {
        _items = @[@"消息提醒"];
    }
    return _items;
}

- (void)setupUI
{
    [self setupNavBackButton];
    
    [self setupFooterForTableView];
    
    [self.tableView registerNib:[UserSwitchSettingCell nib] forCellReuseIdentifier:UserSwitchSettingCellIdentifier];
    [self.tableView registerNib:[ConfirmButtonCell nib] forCellReuseIdentifier:ConfirmButtonCellIdentifier];
    
    self.title = @"设置";
}

#pragma mark UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return indexPath.row ==0 ? kNormalCellHeight : kConfirmButtonCellHeight;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return kUserCenterSectionHeaderHeight;
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0) {
        UserSwitchSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:UserSwitchSettingCellIdentifier
                                                               forIndexPath:indexPath];

        
        cell.titleLabel.text = self.items[indexPath.row];
        return cell;
        
    }else{
        ConfirmButtonCell *cell = [tableView dequeueReusableCellWithIdentifier:ConfirmButtonCellIdentifier
                                                                  forIndexPath:indexPath];
        
        [cell.confirmButton setTitle:@"退出登录" forState:UIControlStateNormal];
        cell.confirm = (ClickBlock)^(){
            [self tapLogoutButton];
        };
        
        return cell;
    }
    
    
    
    
}
- (void)tapLogoutButton
{
    //[self performSegueWithIdentifier:@"ShowForgetPasswordViewController" sender:self];
    [YuNetworkStatusUtilities showProgressMessage:NETWORK_LOADING];
    [self.userSettingsEngine userLogout];
}


/**
 *  Logic Modal For User Settings
 *
 *  @return
 */

#pragma mark - Call Back
- (void)userLogoutFinish:(NSNotification*) notification
{
    
    [YuNetworkStatusUtilities dismissHUD];
    NSDictionary *messageDic = [notification object];//获取到传递的对象
    
    if ([messageDic[@"isSuccess"] boolValue]) {
        
        UIAlertController *x = [UIAlertController alertWithTitle:@"提示" andMessage:@"您已经退出登录" andBlock:^(UIAlertAction *action ) {
            if ([action.title isEqualToString:@"好的"]) {
                [self performSegueWithIdentifier:@"ShowLoginFromSettingsLogout" sender:self];
            }
            
        }  type:@"confirm"];
        [self presentViewController:x animated:YES completion:nil];
        
    }else{
        [self showNetWorkError:messageDic[@"message"]];
        
    }
   
    
}


#pragma mark - Life Cycle
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userLogoutFinish:) name:@"UserLogoutFinish" object:nil];
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UserLogoutFinish" object:nil];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
